from django.contrib import admin
from .models import (
    QuitSmokingProfile, DailyRecord, Milestone, 
    UserMilestone, HealthTip, CravingLog,
    ChatSession, ChatMessage, AIKnowledgeBase
)


@admin.register(QuitSmokingProfile)
class QuitSmokingProfileAdmin(admin.ModelAdmin):
    list_display = ['user', 'quit_date', 'days_quit', 'cigarettes_per_day', 'money_saved']
    list_filter = ['quit_date', 'cigarettes_per_day']
    search_fields = ['user__username', 'user__email']
    readonly_fields = ['days_quit', 'money_saved', 'cigarettes_not_smoked', 'created_at', 'updated_at']
    
    fieldsets = (
        ('基本信息', {
            'fields': ('user', 'quit_date', 'motivation')
        }),
        ('吸烟习惯', {
            'fields': ('cigarettes_per_day', 'cigarettes_per_pack', 'price_per_pack')
        }),
        ('统计信息', {
            'fields': ('days_quit', 'money_saved', 'cigarettes_not_smoked'),
            'classes': ('collapse',)
        }),
        ('时间信息', {
            'fields': ('created_at', 'updated_at'),
            'classes': ('collapse',)
        }),
    )


@admin.register(DailyRecord)
class DailyRecordAdmin(admin.ModelAdmin):
    list_display = ['user', 'date', 'mood', 'cravings_intensity', 'relapsed', 'cigarettes_smoked']
    list_filter = ['date', 'mood', 'relapsed', 'cravings_intensity']
    search_fields = ['user__username', 'notes']
    date_hierarchy = 'date'
    ordering = ['-date']
    
    fieldsets = (
        ('基本信息', {
            'fields': ('user', 'date', 'mood')
        }),
        ('烟瘾情况', {
            'fields': ('cravings_intensity', 'relapsed', 'cigarettes_smoked')
        }),
        ('备注', {
            'fields': ('notes',)
        }),
    )


@admin.register(Milestone)
class MilestoneAdmin(admin.ModelAdmin):
    list_display = ['name', 'days', 'icon']
    list_editable = ['days', 'icon']
    ordering = ['days']
    search_fields = ['name', 'description']


@admin.register(UserMilestone)
class UserMilestoneAdmin(admin.ModelAdmin):
    list_display = ['user', 'milestone', 'achieved_date']
    list_filter = ['milestone', 'achieved_date']
    search_fields = ['user__username', 'milestone__name']
    date_hierarchy = 'achieved_date'
    ordering = ['-achieved_date']


@admin.register(HealthTip)
class HealthTipAdmin(admin.ModelAdmin):
    list_display = ['title', 'category', 'is_active', 'created_at']
    list_filter = ['category', 'is_active', 'created_at']
    search_fields = ['title', 'content']
    list_editable = ['is_active']
    ordering = ['-created_at']


@admin.register(CravingLog)
class CravingLogAdmin(admin.ModelAdmin):
    list_display = ['user', 'datetime', 'intensity', 'trigger', 'successful', 'duration_minutes']
    list_filter = ['trigger', 'successful', 'intensity', 'datetime']
    search_fields = ['user__username', 'coping_method', 'notes']
    date_hierarchy = 'datetime'
    readonly_fields = ['datetime']
    ordering = ['-datetime']
    
    fieldsets = (
        ('基本信息', {
            'fields': ('user', 'datetime', 'intensity')
        }),
        ('烟瘾详情', {
            'fields': ('trigger', 'duration_minutes', 'successful')
        }),
        ('应对方法', {
            'fields': ('coping_method', 'notes')
        }),
    )


@admin.register(ChatSession)
class ChatSessionAdmin(admin.ModelAdmin):
    list_display = ['user', 'title', 'created_at', 'updated_at', 'is_active']
    list_filter = ['is_active', 'created_at', 'updated_at']
    search_fields = ['user__username', 'title']
    readonly_fields = ['created_at', 'updated_at']
    ordering = ['-updated_at']
    
    # 在AI对话会话管理中显示相关的消息
    inlines = []
    
    fieldsets = (
        ('基本信息', {
            'fields': ('user', 'title', 'is_active')
        }),
        ('时间信息', {
            'fields': ('created_at', 'updated_at'),
            'classes': ('collapse',)
        }),
    )
    
    # 添加快捷操作
    actions = ['activate_sessions', 'deactivate_sessions']
    
    def activate_sessions(self, request, queryset):
        updated = queryset.update(is_active=True)
        self.message_user(request, f'已激活 {updated} 个会话')
    activate_sessions.short_description = '激活选中的会话'  # type: ignore[assignment]
    
    def deactivate_sessions(self, request, queryset):
        updated = queryset.update(is_active=False)
        self.message_user(request, f'已停用 {updated} 个会话')
    deactivate_sessions.short_description = '停用选中的会话'  # type: ignore[assignment]


class ChatMessageInline(admin.TabularInline):
    model = ChatMessage
    extra = 0
    readonly_fields = ['created_at', 'response_time']
    fields = ['message_type', 'content', 'feature', 'is_helpful', 'created_at']
    

@admin.register(ChatMessage)
class ChatMessageAdmin(admin.ModelAdmin):
    list_display = ['session', 'message_type', 'feature', 'content_preview', 'created_at', 'is_helpful']
    list_filter = ['message_type', 'feature', 'created_at', 'is_helpful']
    search_fields = ['session__user__username', 'content']
    readonly_fields = ['created_at', 'response_time']
    ordering = ['-created_at']
    
    def content_preview(self, obj):
        return obj.content[:50] + ('...' if len(obj.content) > 50 else '')
    content_preview.short_description = '内容预览'  # type: ignore[assignment]
    
    fieldsets = (
        ('基本信息', {
            'fields': ('session', 'message_type', 'feature')
        }),
        ('消息内容', {
            'fields': ('content',)
        }),
        ('评价信息', {
            'fields': ('is_helpful', 'response_time', 'confidence_score')
        }),
        ('时间信息', {
            'fields': ('created_at',),
            'classes': ('collapse',)
        }),
    )


@admin.register(AIKnowledgeBase)
class AIKnowledgeBaseAdmin(admin.ModelAdmin):
    list_display = ['title', 'category', 'priority', 'is_active', 'created_at']
    list_filter = ['category', 'is_active', 'priority', 'created_at']
    search_fields = ['title', 'content', 'keywords']
    list_editable = ['priority', 'is_active']
    ordering = ['-priority', '-created_at']
    
    fieldsets = (
        ('基本信息', {
            'fields': ('title', 'category', 'priority', 'is_active')
        }),
        ('内容信息', {
            'fields': ('content', 'keywords')
        }),
        ('时间信息', {
            'fields': ('created_at', 'updated_at'),
            'classes': ('collapse',)
        }),
    )
    
    readonly_fields = ['created_at', 'updated_at']



